import store from "../../../store";
import LinkedList from "../../../helper/LinkedList";
import mergeRouter from "../../../utils/settings/mergeRouter";
//返回页面初始化的obj
const getPageObj = (type, id) => {
    return {
        config: {
            id,
            type
        },
        header: new LinkedList(),
        main: new LinkedList(),
        footer: new LinkedList()
    }
}
//返回获取存储自定义页面的数据（specialtemplateforaddpages）
const getSpecialTemplateParams = () => {
    return store.getters["gw/getSpecialTemplateParams"];
}
export default class CustPageUtil {
    static  saveToDB (context, p) {
        let success = (data) => {
            let specialTemplate = data.find(item => item.tpDesc === "specialtemplateforaddpages");
            //重置整个模板list
            context.resetTemplateParams(data);
            //重置特殊模板的数据
            store.commit("gw/setSpecialTemplate", JSON.parse(specialTemplate.tpConfig));
            store.commit("gw/setSpecialTemplateParams", specialTemplate);
            store.commit("gw/setNeedUploadSpecialTemplate", false)
            //此时动态增加路由以便不刷新可以注入路由
            let mergeRouterSpecial  = mergeRouter();
            context.$router.options.routes[4].children.push(...mergeRouterSpecial);
            context.$router.addRoutes(context.$router.options.routes);
            setTimeout(()=>{
                alert("操作成功");
            },500)
        }
        let fail = (errMsg = "操作失败") => {
            // alert(errMsg);
        }
        context.updateDomTreeToDB(success, fail, p);
    }
    //向数据库新增一条自建页面的数据
    static insert(context, pageArray) {
        let needUploadTemplate = store.getters["gw/getNeedUploadSpecialTemplate"]
        let templateParams = getSpecialTemplateParams();
        let domTree;
        if (needUploadTemplate) {
            //需要上传一套专门用来存储添加页面的模板数据
            templateParams = {
                tpConfig: {config: {}},
                tpDesc: "specialtemplateforaddpages",
                storeCode: localStorage.getItem('storeCode'),
                enterpriseCode: window.api.enterpriseCode,
            }
            domTree = templateParams.tpConfig;
        } else {
            domTree = JSON.parse(templateParams.tpConfig);
        }
        //校验边界
        if (!domTree.config) {
            domTree.config = {
                pages: []
            }
        } else if (!domTree.config.pages) {
            domTree.config.pages = [];
        }
        //添加新增的页面对象
        domTree.config.pages = pageArray;
        //映射新增页面对象的dom结构
        let pageObj = getPageObj(pageArray[0].type, pageArray[0].id);
        domTree[pageArray[0].id] = pageObj;
        templateParams.tpConfig = JSON.stringify(domTree);
        CustPageUtil.saveToDB(context, templateParams);
    }
    
    static update(context, pageArray) {
        let domTree = JSON.parse(getSpecialTemplateParams().tpConfig);
        //重新赋值
        domTree.config.pages = [].concat(pageArray);
        let p = {
            ...getSpecialTemplateParams(),
            tpConfig: JSON.stringify(domTree)
        }
        CustPageUtil.saveToDB(context, p);
    }

    static delete(context, pageArray) {
        CustPageUtil.update(context, pageArray);
    }
}